package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.HistoryAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.ActivityHistoryBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.apis.base.JtwzdmQueryBean;
import stark.common.basic.event.EventStatProxy;

public class HistoryActivity extends BaseAc<ActivityHistoryBinding> {

    private List<JtwzdmQueryBean> listShow = new ArrayList<>();
    private HistoryAdapter historyAdapter;
    private Dialog myDeleteDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_history;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivHistoryBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivHistoryDelete.setOnClickListener(this);
        mDataBinding.rvHistoryList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        historyAdapter=new HistoryAdapter();
        mDataBinding.rvHistoryList.setAdapter(historyAdapter);
        //清空弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivHistoryDelete:
                if (listShow.size()==0) return;
                myDeleteDialog.show();
                break;
            case R.id.ivDialogDeleteRight:
                listShow.clear();
                AppSpUtils.saveHistoryList(listShow);
                getData();
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteCancel:
                myDeleteDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData(){
        listShow.clear();
        List<JtwzdmQueryBean> listSp = AppSpUtils.getHisotryList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            historyAdapter.setList(listShow);
            mDataBinding.rvHistoryList.setVisibility(View.VISIBLE);
            mDataBinding.tvHistoryNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvHistoryList.setVisibility(View.GONE);
            mDataBinding.tvHistoryNoData.setVisibility(View.VISIBLE);
        }
    }

    //清空弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView cancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogDeleteRight);
        TextView text=inflate.findViewById(R.id.tvDialogDeleteText);
        text.setText(getString(R.string.clear_history));
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}